from django.db import models
from utils.modelMixin import timeFiledMixin, statusFiledMixin


class ElementPositionStrategy(
    timeFiledMixin.CreateTimeFiled,
    timeFiledMixin.UpdateTimeFiled,
    statusFiledMixin,
    models.Model,
):
    STRATEGY_CHOICES = ((1, "策略一"), (2, "策略二"))
    name = models.CharField(
        max_length=16, verbose_name="名称", help_text="策略名称", unique=True, db_index=True
    )
    type = models.IntegerField(
        choices=STRATEGY_CHOICES, db_index=True, verbose_name="类型", help_text="策略类型"
    )
    weights = models.IntegerField(verbose_name="权重", help_text="策略执行优先级")
    description = models.CharField(
        max_length=128, null=True, blank=True, verbose_name="描述", help_text="策略描述信息"
    )

    class Meta:
        db_table = "ui_position_strategy"
        verbose_name = "元素定位策略"
        verbose_name_plural = verbose_name
        ordering = ["name"]
        abstract = True

    def __str__(self):
        return self.name


class ElementPosition(statusFiledMixin.ActiveFiled, models.Model):
    method = models.CharField(
        max_length=128, verbose_name="定位方法", help_text="执行元素定位框架支持的定位方法"
    )
    enum = models.CharField(
        max_length=128,
        verbose_name="枚举值",
        blank=True,
        null=True,
        help_text="部分定位方法存在枚举值，例如: enabled: Ture or False，传入数据格式: ((0, False),(1, True))",
    )
    description = models.CharField(max_length=128, verbose_name="描述", default=None)

    class Meta:
        db_table = "ele_position"
        verbose_name = "定位方式"
        verbose_name_plural = verbose_name
        ordering = ["id"]
        abstract = True

    def __str__(self):
        return self.method


class ElementToPosition(models.Model):
    element = models.ForeignKey(
        "ElementInfo",
        verbose_name="元素",
        on_delete=models.CASCADE,
        related_name="element_position",
    )
    position = models.ForeignKey(
        "ElePosition",
        verbose_name="定位方式",
        on_delete=models.CASCADE,
        related_name="position_values",
    )
    values = models.CharField(max_length=128, verbose_name="路径")

    class Meta:
        db_table = "ele_to_position"
        verbose_name = "元素定位方法管理"
        verbose_name_plural = verbose_name
        abstract = True

    def __str__(self):
        return f"{self.element.name} -> {self.position.method}: {self.values}"


class Element(models.Model):
    name = models.CharField(
        max_length=32, db_index=True, verbose_name="名称", help_text="元素名称"
    )
    index = models.CharField(
        max_length=32, db_index=True, verbose_name="索引", help_text="元素关键字"
    )
    min_version = models.IntegerField(
        verbose_name="最小生效版本", null=True, blank=True, default=340
    )
    active = models.ForeignKey(
        "Page",
        blank=True,
        null=True,
        verbose_name="页面",
        help_text="元素归属页面",
        on_delete=models.CASCADE,
        related_name="fk_active",
    )
    strategy = models.ManyToManyField("EleStrategy", verbose_name="查找策略", blank=True)
    description = models.CharField(max_length=64, verbose_name="描述")
    status = models.IntegerField(verbose_name="状态", choices=STATUS, default=False)

    create_time = models.DateTimeField(auto_now_add=True, verbose_name="创建时间")
    update_time = models.DateTimeField(auto_now=True, verbose_name="更新时间")

    class Meta:
        db_table = ""
        verbose_name = "元素信息管理"
        verbose_name_plural = verbose_name
        unique_together = ("index", "min_version")
        abstract = True

    def __str__(self):
        return self.name


class Page(models.Model):
    name = models.CharField(max_length=32, verbose_name="名称")
    active = models.CharField(
        max_length=64, verbose_name="active", help_text="应用页面active", unique=True
    )
    application = models.ForeignKey(
        "App",
        verbose_name="应用",
        on_delete=models.CASCADE,
        default=None,
        related_name="fk_app",
    )
    description = models.CharField(max_length=128, verbose_name="描述", default=None)

    class Meta:
        db_table = "app_page"
        verbose_name = "APP页面管理"
        verbose_name_plural = verbose_name
        ordering = ["id"]
        abstract = True

    def __str__(self):
        return f"{self.name}-{self.active}"


class App(
    statusFiledMixin.StatusFiled,
    timeFiledMixin.UpdateTimeFiled,
    timeFiledMixin.CreateTimeFiled,
    models.Model,
):
    name = models.CharField(
        max_length=32, verbose_name="应用名称", help_text="应用名称", db_index=True
    )
    package = models.CharField(
        max_length=64, verbose_name="包名", help_text="应用报名", unique=True, db_index=True
    )
    description = models.CharField(
        max_length=128, verbose_name="应用描述", help_text="应用描述"
    )

    class Meta:
        db_table = "app_info"
        verbose_name = "APP信息管理"
        verbose_name_plural = verbose_name
        abstract = True

    def __str__(self):
        return "{}-{}".format(self.name, self.package)
